<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程id" prop="courseId">
      <el-input v-model="dataForm.courseId" placeholder="课程id"></el-input>
    </el-form-item>
    <el-form-item label="学生id" prop="studentId">
      <el-input v-model="dataForm.studentId" placeholder="学生id"></el-input>
    </el-form-item>
    <el-form-item label="教室" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="教室"></el-input>
    </el-form-item>
    <el-form-item label="座位号" prop="seatNumber">
      <el-input v-model="dataForm.seatNumber" placeholder="座位号"></el-input>
    </el-form-item>
    <el-form-item label="考试时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="考试时间"></el-input>
    </el-form-item>
    <el-form-item label="监考老师" prop="invigilator">
      <el-input v-model="dataForm.invigilator" placeholder="监考老师"></el-input>
    </el-form-item>
    <el-form-item label="学期" prop="semester">
      <el-input v-model="dataForm.semester" placeholder="学期"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          courseId: '',
          studentId: '',
          roomId: '',
          seatNumber: '',
          time: '',
          invigilator: '',
          semester: ''
        },
        dataRule: {
          courseId: [
            { required: true, message: '课程id不能为空', trigger: 'blur' }
          ],
          studentId: [
            { required: true, message: '学生id不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '教室不能为空', trigger: 'blur' }
          ],
          seatNumber: [
            { required: true, message: '座位号不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '考试时间不能为空', trigger: 'blur' }
          ],
          invigilator: [
            { required: true, message: '监考老师不能为空', trigger: 'blur' }
          ],
          semester: [
            { required: true, message: '学期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/eascourse/examinationarrangement/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.courseId = data.examinationArrangement.courseId
                this.dataForm.studentId = data.examinationArrangement.studentId
                this.dataForm.roomId = data.examinationArrangement.roomId
                this.dataForm.seatNumber = data.examinationArrangement.seatNumber
                this.dataForm.time = data.examinationArrangement.time
                this.dataForm.invigilator = data.examinationArrangement.invigilator
                this.dataForm.semester = data.examinationArrangement.semester
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/eascourse/examinationarrangement/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'courseId': this.dataForm.courseId,
                'studentId': this.dataForm.studentId,
                'roomId': this.dataForm.roomId,
                'seatNumber': this.dataForm.seatNumber,
                'time': this.dataForm.time,
                'invigilator': this.dataForm.invigilator,
                'semester': this.dataForm.semester
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
